class GwtBuyerController<GwtController
  @@LOG_FLAG="GwtBuyerController"
  def index
    user_id = session[:id]
    domain_login_id = session[:domain_login_id]
    if user_id==nil
      render :text => '请通过阿里平台登录后'
      return
    end
    start = 0
    limit = 6

    orders = WWW::Tkd::Gold::Model::TbOrder.find_buyer_on_page(domain_login_id,start.to_i,limit.to_i)
    @tb_orders=Array.new
    orders.each do|item|
      @tb_orders.push(WWW::Tkd::Gold::Model::GwtBuyerOrder.init_by_order(item,user_id))
    end
    result = Hash.new
    result['offset']=start.to_i
    result['count']=WWW::Tkd::Gold::Model::TbOrder.count_buyer user_id
    result['items']=@tb_orders
    render :text => result.to_json
  end
  #主视图
  def view
    #预先判定是否是淘宝用户
    domain_login_id=params['domain_login_id']
    if domain_login_id!=nil
      begin
        conv = Iconv.new("UTF-8","GB2312")
        domain_login_id = conv.iconv(domain_login_id)
      rescue
      end
      if !checkname(domain_login_id)
        render :text =>'尊敬的用户，淘快递仅支持淘宝用户，请切换其他帐号，谢谢！'
        return
      end
      user_id = params['user_id']
      app_instance_id = params['app_instance_id']
      hash_code=params['hash_code']

      tb_user=WWW::Tkd::Gold::Model::TbUser.save_from_aliww(domain_login_id,user_id,app_instance_id,hash_code,false,false)#最后的boolean标示是否来源BOPS
      if tb_user==nil
        render :text =>'尊敬的用户，本次登录为非法'
        return
      end
      session[:user_id]=tb_user.user_id
      session[:id]=tb_user.id
      session[:sip_sessionid]=tb_user.sip_sessionid
      session[:domain_login_id]=tb_user.domain_login_id

      session[:sip_sessionid]=tb_user.sip_sessionid
      session[:domain_login_id]=tb_user.domain_login_id

      app_id='22533'
      cert_code='7d7b6da008d911de824dcef6c0526f2a'
      sip_url_pre='http://sip.alisoft.com/sip/rest'
      isv_url_pre='http://buyer.taokuaidi.cn/gwt_buyer/success'
      WWW::Ali.set_app_id app_id
      WWW::Ali.set_cert_code cert_code
      WWW::Ali.set_sip_url_pre sip_url_pre
      WWW::Ali.set_isv_url_pre isv_url_pre

      if tb_user.is_newer#标示第一次注册用户
        refresh_sip_sessionid
        redirect_to '/buyerGuide.html'
        return
      end
    end
    #如当前在途宝贝为0,给用户提示信息
    count = WWW::Tkd::Gold::Model::TbBuyerOrder.count(:conditions => "user_id = #{session[:id]}")
    if count==0
      refresh_sip_sessionid
      @isAPIExtendTime = false
    else
      @isAPIExtendTime = is_valid_login
    end
    user_id = session[:id]
    if user_id==nil
      render :text => '请通过阿里平台登录后'
      return
    end
  end

end
